package in

import (
	"encoding/json"
	"fmt"
	"log"
	"os"
	"strings"

	"github.com/fatih/color"
	"github.com/jedib0t/go-pretty/table"
)

type DebugLog bool

var Debug DebugLog
var dbgLog = log.New(os.Stdout, "[DEBUG] ", log.Ltime)
var mypid = os.Getpid()

var FGCOLORS = []func(a ...interface{}) string{
	color.New(color.FgYellow, color.Bold).SprintFunc(),
	color.New(color.FgRed, color.Bold).SprintFunc(),
	color.New(color.FgGreen, color.Bold).SprintFunc(),
	color.New(color.FgBlue, color.Bold).SprintFunc(),
}
var BGCOLORS = []func(a ...interface{}) string{
	color.New(color.BgYellow, color.Bold).SprintFunc(),
	color.New(color.BgRed, color.Bold).SprintFunc(),
	color.New(color.BgGreen, color.Bold).SprintFunc(),
	color.New(color.BgBlue, color.Bold).SprintFunc(),
}

func ColorT(args Datas) {
	t := table.NewWriter()
	t.SetOutputMirror(os.Stdout)
	all := 0
	var headers table.Row
	for no, arg := range args {
		if b, err := json.Marshal(arg); err == nil {
			var data map[string]interface{}
			// yellow := FGCOLORS[0]
			var values table.Row
			if err := json.Unmarshal(b, &data); err == nil {
				if no == 0 {
					for k := range data {
						values = append(values, fmt.Sprint(data[k]))
						headers = append(headers, fmt.Sprint(k))
					}
				} else {
					for _, k := range headers {
						values = append(values, fmt.Sprint(data[k.(string)]))
					}

				}
				if no == 0 {
					t.AppendHeader(headers)
				}
				t.AppendRow(values)

			}
		}
		all = no + 1
	}
	t.AppendFooter(table.Row{"Total", all})
	t.Render()
}

func ColorD(args interface{}, join ...string) {

	if b, err := json.Marshal(args); err == nil {
		var data map[string]interface{}
		// yellow := FGCOLORS[0]
		if err := json.Unmarshal(b, &data); err == nil {
			var S []string
			c := 0
			for k, v := range data {
				// ColorD(data)
				S = append(S, fmt.Sprint(k, ": ", FGCOLORS[c](v)))
				c++
				c %= len(BGCOLORS)
			}
			if len(join) == 0 {
				fmt.Println(strings.Join(S, "\n"))
			}

		}
	}
}

func ColorL(args ...interface{}) {
	yellow := color.New(color.FgWhite, color.BgYellow).SprintFunc()
	red := color.New(color.FgWhite, color.BgRed).SprintFunc()
	green := color.New(color.FgWhite, color.BgGreen).SprintFunc()
	blue := color.New(color.FgWhite, color.BgBlue).SprintFunc()
	var S string
	useR := false
	if args[len(args)-1] == "\r" {
		useR = true
		args = args[:len(args)-1]
	}
	for i, arg := range args {
		switch i % 4 {
		case 0:
			S += green(fmt.Sprint("[", arg, "]"))
		case 1:
			S += yellow(fmt.Sprint("[", arg, "]"))
		case 2:
			S += blue(fmt.Sprint("[", arg, "]"))
		case 3:
			S += red(fmt.Sprint("[", arg, "]"))
		}
	}
	if useR {
		fmt.Print(S, "                   \r")
	} else {
		fmt.Println(S)
	}

}

func ColorS(args ...string) {
	yellow := color.New(color.FgWhite, color.BgYellow).SprintFunc()
	red := color.New(color.FgWhite, color.BgRed).SprintFunc()
	green := color.New(color.FgWhite, color.BgGreen).SprintFunc()
	blue := color.New(color.FgWhite, color.BgBlue).SprintFunc()
	var S string
	useR := false
	if args[len(args)-1] == "\r" {
		useR = true
		args = args[:len(args)-1]
	}
	for i, arg := range args {
		switch i % 4 {
		case 0:
			S += green(arg)
		case 1:
			S += yellow(arg)
		case 2:
			S += blue(arg)
		case 3:
			S += red(arg)
		}
	}
	if useR {
		fmt.Print(S, "                   \r")
	} else {
		fmt.Println(S)
	}

}
